import json

from django import forms
from django.utils.translation import ugettext_lazy as _


class ErrorCode(object):
    UNEXPECTED_ERROR = 1
    HTTP_METHOD_NOT_ALLOWED = 2
    LOGIN_REQUIRED_ERROR = 3
    INPUT_FORMAT_ERROR = 4
    HRIS_STAFF_NOT_EXIST = 9
    HRIS_STAFF_DEACTIVATED = 31
    HRIS_DUPLICATE_ENTRY = 32
    HRIS_PERMISSON_ERROR = 33


class ApiError(Exception):
    def __str__(self):
        return self.debug
    def __init__(self, msg='', debug=''):
        self.msg = msg
        self.debug = debug

class ApiUnexpectedError(ApiError):
    value = ErrorCode.UNEXPECTED_ERROR
    def __init__(self, msg=_("Unknown error"), debug="Unknown error"):
        super(ApiUnexpectedError, self).__init__(msg, debug)


class ApiHttpMethodNotAllowed(ApiError):
    value = ErrorCode.HTTP_METHOD_NOT_ALLOWED
    def __init__(self,
                 msg=_("Http Method Not Allowed"),
                 debug="Http Method Not Allowed"):
        super(ApiHttpMethodNotAllowed, self).__init__(msg, debug)

class ApiLoginRequiredError(ApiError):
    value = ErrorCode.LOGIN_REQUIRED_ERROR

    def __init__(self,
                 msg=_("Login Required"),
                 debug="Login Required"):
        super(ApiLoginRequiredError, self).__init__(msg, debug)

class ApiInputFormatError(ApiError):
    value = ErrorCode.INPUT_FORMAT_ERROR

    def __init__(self,
                 msg=_("Input format is invalid."),
                 debug="Input format is invalid.",
                 *args, **kwargs):
        super(ApiInputFormatError, self).__init__(*args, **kwargs)
        self.msg = unicode(msg)
        self.debug = debug

class ApiStaffDeactivated(ApiError):
    value = ErrorCode.HRIS_STAFF_DEACTIVATED

    def __init__(self,
                 msg=_("Staff is deactivated"),
                 debug="Staff is deactivated"):
        super(ApiStaffDeactivated, self).__init__(msg, debug)

class ApiStaffNotExist(ApiError):
    value = ErrorCode.HRIS_STAFF_NOT_EXIST

    def __init__(self,
                 msg=_("Staff does not exist"),
                 debug="Staff does not exist"):
        super(ApiStaffNotExist, self).__init__(msg, debug)


class ApiDuplicateEntry(ApiError):
    value = ErrorCode.HRIS_DUPLICATE_ENTRY

    def __init__(self,
                 msg=_("Entry Duplicated"),
                 debug="Entry Duplicated"):
        super(ApiDuplicateEntry, self).__init__(msg, debug)

class ApiPermissionError(ApiError):
    value = ErrorCode.HRIS_PERMISSON_ERROR

    def __init__(self,
                 msg=_("Permission Denied"),
                 debug="Permission Denied"):
        super(ApiPermissionError, self).__init__(msg, debug)


